package com.hyy.springbootblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyy.springbootblog.common.exception.BlogException;
import com.hyy.springbootblog.common.pojo.dataobject.BlogInfo;
import com.hyy.springbootblog.common.pojo.dataobject.UserInfo;
import com.hyy.springbootblog.common.pojo.request.UserLoginParam;
import com.hyy.springbootblog.common.pojo.response.UserInfoResponse;
import com.hyy.springbootblog.common.pojo.response.UserLoginResponse;
import com.hyy.springbootblog.common.utils.BeanConver;
import com.hyy.springbootblog.common.utils.JwtUtil;
import com.hyy.springbootblog.common.utils.SecurityUtil;
import com.hyy.springbootblog.mapper.BlogInfoMapper;
import com.hyy.springbootblog.mapper.UserInfoMapper;
import com.hyy.springbootblog.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service//实现类里面用
public class UserServiceImpl implements UserService {

    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;

    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;


    @Override
    public UserLoginResponse login(UserLoginParam userLoginParam) {
        //判断用户是否存在

        //根据用户名查询用户信息
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getUserName, userLoginParam.getUsername())
                .eq(UserInfo::getDeleteFlag, 0));
        if (userInfo == null) {
            System.out.println(userLoginParam.getUsername());
            System.out.println(userLoginParam.getPassword());
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            throw new BlogException("用户不存在");
        }

        //用户存在校验密码是否正确
        if (!SecurityUtil.verify(userLoginParam.getPassword(), userInfo.getPassword())) {
            System.out.println(userLoginParam.getPassword());
            System.out.println(userInfo.getPassword());

            throw new BlogException("密码错误");
        }

        //密码正确
        log.error("密码正确已经判断了：：：：：：：：：：：：：：：：：：：：");
        UserLoginResponse response = new UserLoginResponse();
        response.setUserId(userInfo.getId());

        //获取token
        //设置载荷
        Map<String, Object> map = new HashMap<>();
        map.put("id", userInfo.getId());
        map.put("username", userInfo.getUserName());
        response.setToken(JwtUtil.getToken(map));
        return response;
    }


    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getDeleteFlag, 0)
                .eq(UserInfo::getId, userId));
        return BeanConver.trans(userInfo);
    }

    //根据博客id查看作者信息
    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {

        //1、根据博客id 找到作者id
        //2、根据作者id 找到作者信息

        //查询到博客信息
        BlogInfo blogInfo = blogInfoMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }

        UserInfo userInfo = selectUserInfoById(blogInfo.getUserId());
        return BeanConver.trans(userInfo);
    }


    //根据用户id查看用户信息
    public UserInfo selectUserInfoById(Integer userId) {
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getDeleteFlag, 0)
                .eq(UserInfo::getId, userId));
        if(userInfo == null) {
            throw new BlogException("用户不存在");
        }
        return userInfo;
    }
}
